// ----------------------------------------
// Filename: Timer.c
// Description: Routines to implement timer
// Author: Anton Jatmiko
// Date: 21 April 2014

#include "Timer.h"

// ----------------------------------------
// Timer_Init
//
// Initialize timer
// Input:
//   none
// Output:
//   none
// Conditions:
//   none
void Timer_Init(void)
{
    // Timer2 Config
    INTCONbits.PEIE = 1;        // Enable all unmasked interrupts
    PIR1bits.TMR2IF = 0;        // Clear timer2 interrupt flag
    PIE1bits.TMR2IE = 1;        // Enable TMR2 to PR2 match interrupt
    IPR1bits.TMR2IP = 1;        // Set timer2 interrupt priority to high

    T2CONbits.TMR2ON = 0;       // Disable timer 2
    T2CONbits.T2CKPS = 0;       // Prescale: 1
    T2CONbits.TOUTPS = 10;      // Postscale: 10
    TMR2 = 0;
    PR2 = 99;                   // Interrupt every 1ms
}

// ----------------------------------------
// Timer_SetInterrupt
//
// Arm or disarm timer
// Input:
//   enable is a boolean indicating whether to enable or disable timer
// Output:
//   none
// Conditions:
//   none
void Timer_SetInterrupt(BOOL enable)
{
    T2CONbits.TMR2ON = enable;       // Enable or disable timer 2
}